package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class DelMRitem
 */ 
public class ManipulateGrid extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ManipulateGrid() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		Widgets wid = new Widgets();
		Connection conn = wid.getConn(true);
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String strSQL = "";
		String oper = request.getParameter("oper");
		String id = "";
		String result = "";
		String currentUser = "842297";
		
		/** 判別需進行何種操作 **/
		if ( "del".equalsIgnoreCase(oper) ) {
			id = request.getParameter("id");
			strSQL = "DELETE FROM bss WHERE bssid=?";
			try {
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, id);
				ps.execute();
				
				wid.keepLogProfile(wid.BSS, wid.DELETE, currentUser, conn, ps);
				result = "刪除了"+id;
			} catch (SQLException e) {
				result = "刪除"+id+"失敗!!\n";
				result += wid.errProcess(e);
			}
		} else if ( "add".equalsIgnoreCase(oper) ) {
			strSQL = "SELECT COUNT(bssid) FROM bss WHERE bssid=?";
			id = request.getParameter("AAAA");
			try {
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, id);
				rs = ps.executeQuery();
				rs.next();
				if ( rs.getInt(1) == 0 ) {
					strSQL = "INSERT INTO bss (bssid,bssname,creator,createdDateTime) VALUES (?,?,?,?)";
					String bssName = request.getParameter("HHHH");
					ps = conn.prepareStatement(strSQL);
					ps.setString(1, id);
					ps.setString(2, bssName);
					ps.setString(3, currentUser);
					ps.setTimestamp(4, wid.currentTimeStamp());
					ps.execute();
					
					wid.keepLogProfile(wid.BSS, wid.ADD, currentUser, conn, ps);
					result = "新增完成 "+id;
				} else {
					result = id+" 已存在 !";		
				}
			} catch (SQLException e) {
				result = "新增"+id+"失敗!!\n\n";
				result += wid.errProcess(e);
			}
		} else if ( "edit".equalsIgnoreCase(oper) ) {
			id = request.getParameter("id");
			String bssName = request.getParameter("HHHH");
			strSQL = "UPDATE bss SET bssname=?,lastUpdator=?,lastUpdatedDateTime=? WHERE bssid=?";
			try {
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, bssName);
				ps.setString(2, currentUser);
				ps.setTimestamp(3, wid.currentTimeStamp());
				ps.setString(4, id);
				ps.execute();
				
				wid.keepLogProfile(wid.BSS, wid.UPDATE, currentUser, conn, ps);
				result = "修改完成 "+id;

			} catch (SQLException e) {
				result = "修改"+id+"失敗!!\n\n";
				result += wid.errProcess(e);
			}
		}

		try {
			if (rs!= null) {rs.close();}
			if (ps!= null) {ps.close();}
			if (conn!= null) {conn.close();}
		} catch (SQLException e) {
			System.out.println(wid.errProcess(e));
		}
		
		response.setContentType("application/text");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(result);		
	}
}